package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IMiguMusicRetentAddStatisticsDao;
import org.jsbd.boss.dao.IMiguMusicSearchDAO;
import org.jsbd.boss.domian.MiguMusicRetentAddStatistics;
import org.jsbd.boss.domian.MiguMusicRetentDoNumStatistics;
import org.jsbd.boss.domian.MiguMusicSearchVo;
import org.jsbd.boss.service.IMiguMusicSearchService;
import org.springframework.stereotype.Service;

@Service("miguMusicSearchService")
public class MiguMusicSearchServiceImpl implements IMiguMusicSearchService {

	@Resource
	private IMiguMusicSearchDAO miguMusicSearchDao;

	@Resource
	private IMiguMusicRetentAddStatisticsDao miguMusicRetentAddStatisticsDao;

	@Override
	public Map<String, Object> getNewTaskExecListPage(DataGridModel page, MiguMusicSearchVo miguMusicSearchVo) {
		return miguMusicSearchDao.selectNewTaskExecListPage(page, miguMusicSearchVo);
	}

	@Override
	public Map<String, Object> getRetentTaskExecList(MiguMusicSearchVo miguMusicSearchVo) {

		// 咪咕留存执行统计
		List<MiguMusicRetentDoNumStatistics> list1 = miguMusicSearchDao.selectRetentTaskExecList(miguMusicSearchVo);
		// 咪咕留存补量执行统计
		List<MiguMusicRetentAddStatistics> list2 = miguMusicRetentAddStatisticsDao.getMiguDayList(miguMusicSearchVo);

		TreeMap<String, MiguMusicRetentDoNumStatistics> map = new TreeMap<>();
		for (MiguMusicRetentDoNumStatistics retent : list1) {
			retent.setBlNum(Integer.valueOf(0));
			map.put(retent.getKey(), retent);
		}

		for (MiguMusicRetentAddStatistics add : list2) {
			MiguMusicRetentDoNumStatistics retent = map.get(add.getKey());
			if (retent == null) {
				map.put(add.getKey(), add.getMiguMusicRetentDoNumStatistics());
			} else {
				retent.setBlNum(add.getDoNum());
			}
		}

		List<MiguMusicRetentDoNumStatistics> list3 = new ArrayList<>(map.size());
		for (Map.Entry<String, MiguMusicRetentDoNumStatistics> entry : map.entrySet()) {
			list3.add(entry.getValue());
		}

		Map<String, Object> results = new HashMap<String, Object>();
		results.put("rows", list3);
		return results;
	}

}
